package com.qdxwx.manage.controller.course;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.data.service.course.CourseAlbumItemService;
import com.qdxwx.data.service.course.CourseAlbumService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.CourseAlbum;
import com.qdxwx.models.course.CourseAlbumItem;
import com.qdxwx.models.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程专集Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/album/item")
public class CourseAlbumItemController extends BaseController
{
    private final String prefix = "course";

    @Autowired
    private CourseAlbumItemService courseAlbumItemService;

    @Autowired
    private CourseAlbumService courseAlbumService;

    @GetMapping()
    public String detail(HttpServletRequest request, ModelMap mmap) {

        List<CourseAlbum> list = courseAlbumService.getList();

        mmap.put("courseAlbum", list);

        return prefix + "/album-item-list";
    }

    /**
     * 查询课程专集列表
     */
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CourseAlbumItem courseAlbumItem, HttpServletRequest request) {
        List<CourseAlbumItem> list = courseAlbumItemService.getList(courseAlbumItem, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(courseAlbumItemService.count(courseAlbumItem));
        return rspData;
    }

    /**
     * 新增课程专集
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<CourseAlbum> list = courseAlbumService.getList();
        mmap.put("courseAlbum", list);
        return prefix + "/album-item-add";
    }

    /**
     * 新增保存课程专集
     */
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Integer reId , String courseIds) {
        //查询类型实体
        CourseAlbum courseAlbum = courseAlbumService.get(reId);
        //查询该类型所属的课程专集
        List<CourseAlbumItem> detailList = courseAlbumItemService.getList(reId);
        //得到原有数量
        int count = detailList == null ? 0 : detailList.size();
        if (courseIds == null || courseIds.trim().length() == 0) {
            return error("请选择课程");
        }
        //得到没有重复的课程ID
        String[] courseArr = handleCourseId(detailList, courseIds);
        count = courseArr.length + count;

        if (count > courseAlbum.getCourseNum()) {
            return error("[" + courseAlbum.getName() + "]所属专集最多只能添加[" + courseAlbum.getCourseNum() + "]个课程");
        }
        if (courseArr.length > 0) {
            for (String s : courseArr) {
                CourseAlbumItem details = new CourseAlbumItem();
                details.setAlbumId(reId);
                details.setCourseId(TKConvert.toInteger(s));
                details.setOrderNum(0);
                courseAlbumItemService.insert(details);
            }
        }
        return success();
    }

    /**
     * 修改排序
     */
    @PostMapping( "/edit")
    @ResponseBody
    public AjaxResult UpdateSort(HttpServletRequest request)
    {
        Integer id = TKConvert.toInteger( request.getParameter("id"));
        Integer orderNum = TKConvert.toInteger(request.getParameter("orderNum"));
        courseAlbumItemService.updateSort(id,orderNum);
        return success();
    }

    /**
     * 删除课程专集
     */
    @Log(title = "课程专集", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(Integer ids)
    {
        courseAlbumItemService.deleteById(ids);
        return success();
    }

    /**
     * 处理重复课程ID
     */
    private String[] handleCourseId(List<CourseAlbumItem> detailList, String courseIds) {
        if (courseIds.trim().startsWith(",")) {
            courseIds = courseIds.trim().substring(1);
        }
        if (courseIds.trim().endsWith(",")) {
            courseIds = courseIds.trim().substring(0, courseIds.trim().length() - 1);
        }
        String[] _arr = courseIds.split(",");

        List<String> arr = new ArrayList<>();
        if (detailList != null && detailList.size() > 0) {
            for (String id : _arr) {
                int _index = 0;
                for (CourseAlbumItem wcd : detailList) {
                    if (Integer.parseInt(id) == wcd.getCourseId()) {
                        _index = 1;
                    }
                }
                if (_index == 0) {
                    arr.add(id);
                }
            }
            String[] courseArr = new String[arr.size()];
            arr.toArray(courseArr);
            return courseArr;
        } else {
            return _arr;
        }
    }
}
